//
//  BaseWebViewCon.swift
//  Base
//
//  Created by shufang zou on 2017/9/21.
//  Copyright © 2017年 敬信. All rights reserved.
//

import UIKit
import Extend


open class BaseWebViewCon: BaseViewCon, UIWebViewDelegate {
    public var webUrl: String!
    public var webTitle: String!
    
    lazy var webView : UIWebView = {
        let webView = UIWebView.init(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height - 64))
        webView.delegate = self
        webView.scalesPageToFit = true
        //不允许弹出系统自带播放器
        webView.allowsInlineMediaPlayback = true
        //h5端能自动播放
        webView.mediaPlaybackRequiresUserAction = false
        return webView
    }()
  
    open override func viewDidDisappear(_ animated: Bool) {
        ProgressHUD.dismiss()
    }
    
    open override func viewWillDisappear(_ animated: Bool) {
        //页面消失时，播放一段空页面。停止视频播放
        webView.loadRequest(URLRequest(url: URL(string: "about:blank")!))
    }
    
    override open func viewDidLoad() {
        super.viewDidLoad()
        showErrorView = true
    }
    
    override open func setViews() {
        ProgressHUD.loading()
        let url = NSURL(string:webUrl)
        let request = NSURLRequest(url:url! as URL)
        webView.loadRequest(request as URLRequest)
        self.view .addSubview(webView)
    }
    
    override open func setNavBar(bar: NavBar) {
        bar.addBackBtn {
            self.popViewCon()
        }
        
        bar.addMidView(text: webTitle)
    }
    
    //webView delegate
    public func webViewDidStartLoad(_ webView: UIWebView) {
        
    }
    
    public func webViewDidFinishLoad(_ webView: UIWebView) {
        ProgressHUD.dismiss()
        
        let str = "document.documentElement.innerHTML"
        var content = webView.stringByEvaluatingJavaScript(from: str)

        //设置视频为内嵌播放
        var phoneVersion = CGFloat((Float(UIDevice.current.systemVersion) ?? 0.0))
        if phoneVersion >= 10.0 {
            content = content?.replacingOccurrences(of: "<video", with: "<video playsinline")
        }
        else {
            content = content?.replacingOccurrences(of: "<video", with: "<video webkit-playsinline")
        }
        
        self.view.bringSubview(toFront: webView)
    }
    
    public func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
        ProgressHUD.dismiss()
        onNetworkDisable(.dis)
    }
    
    override open func reTryReqest() {
        ProgressHUD.loading()
        let url = NSURL(string:webUrl)
        let request = NSURLRequest(url:url! as URL)
        webView.loadRequest(request as URLRequest)
    }

    override open func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
